<?PHP

class AffiliateBO extends BusinessObject{

   var $cBusiness_Types = array(
        0=>'Sole Proprietorship',
        1=>'Partnership',
        2=>'Corporation',
        3=>'LLC',
        4=>'Other'
      );

  /* private function using in constructors*/
  function setAttributes(){
    $this->fields   = array(
        'id'        =>  'affID',
        'firstName' =>  'affFirstName',
        'lastName'  =>  'affLastName',
        'company'   =>  'affCompanyName',
        'company_website'   =>  'affCompWebsite',
        'login'     =>  'affLogin',
        'password'  =>  'affPassword',
		'apikey'		=>	'affApiKey',
        'btype'     =>  'affBussinessType',
        'taxID'     =>  'affTaxID',

        'mailingAddr'=> 'affMailingAddr',
        'apt'       =>  'affApt',
        'city'      =>  'affCity',
        'state'     =>  'affState',
        'zip'       =>  'affZip',
        'country'   =>  'affCountry',

        'phone'     =>  'affPhone',
        'fax'       =>  'affFax',
        'email'     =>  'affEmail',
        
        'website'   =>  'affWebsite',
        'compType'  =>  'affCompType',
        'amount'    =>  'affAmount', // compensation Rate
        'compPer'   =>  'affCompPer',
        'template'  =>  'affTemplate',

        'discountType'   =>  'affDiscountType',   //discout for affiliate's clients
        'discountAmount' =>  'affDiscountAmount', // amount of discount

        'confirmed' =>  'affConfirmed',
        'blocked'   =>  'affBlocked',
        'show_related' => 'affShowRelated',
        'is_concierge' => 'affIsConcierge',
        'is_xml' => 'affIsXML',
    	'override_specials' => 'affOverrideSpecials',
    	'has_ccreader' => 'affHasCCReader',
        'main_account' => 'affMainAccount'                       
		);

    BusinessObject::setAttributes();
  }
  /* constuctor */
  function AffiliateBO(){
    /** super(tableName) */
    BusinessObject::BusinessObject("affiliates");
    $this->setAttributes();
  }

  function GetBTypesSelect($iSelected=0)
  {
    return $this->BuildOptionsHTML($this->cBusiness_Types, $iSelected);
  }

  /*  function checkLogin  
      if username exist and not this user's then return false else true.
  */
	function checkLogin(){
		$u = new AffiliateBO();
		$u->login = $this->login;
		$u->Load(0,1);
		if($u->Next()){
			if($u->id != $this->id)
			return(false);
		}
		return(true);
	}

  /*  function checkEmail  
      if email already exist and not this user's then return false else true.
  */
	function checkEmail(){
		$u = new AffiliateBO();
		$u->email = $this->email;
		$u->deleted = 0;
		$u->Load(0,1);
		if($u->Next()){
			if($u->id != $this->id)
			return(false);
		}
		return(true);
  }

	/*  Save  */
	function Save($userID){
		if(is_numeric($userID) && $userID<>0) {
			$this->id = "$userID";
			if(!($this->checkLogin())) return (" User with Login '{$this->login}' already exist");
			if(!($this->checkEmail())) return (" User with Email '{$this->email}' already exist");
			$this->Update("{$this->fields[id]} = '$userID'");  
		} else{
			if(!($this->checkLogin())) return (" User with Login '{$this->login}' already exist");
			if(!($this->checkEmail())) return (" User with Email '{$this->email}' already exist");
			$this->Insert();
			$this->id = $this->getInsertID();
		}
	}


    function BuildOptionsHTML($aSource, $xSelected)
    {
      $sResult='';
      foreach ($aSource as $sName=>$sValue)
    	{
    		if ($sName==$xSelected)
    		{
    			$sResult.='<OPTION value="'.$sName.'" selected>'.$sValue.'</OPTION>';
    		}
    		else 
    		{
    			$sResult.='<OPTION value="'.$sName.'">'.$sValue.'</OPTION>';
    		}
    	}
    	return $sResult;
    }


}
?>
